package com.ruoyi.product.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.product.domain.ProductSwitch;
import com.ruoyi.product.service.IProductSwitchService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 开关机信息Controller
 * 
 * @author ruoyi
 * @date 2023-01-12
 */
@Controller
@RequestMapping("/product/switch")
public class ProductSwitchController extends BaseController
{
    private String prefix = "product/switch";

    @Autowired
    private IProductSwitchService productSwitchService;

    @RequiresPermissions("product:switch:view")
    @GetMapping()
    public String switchOnOff()
    {
        return prefix + "/switch";
    }

    /**
     * 查询开关机信息列表
     */
    @RequiresPermissions("product:switch:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductSwitch productSwitch)
    {
        startPage();
        List<ProductSwitch> list = productSwitchService.selectProductSwitchList(productSwitch);
        return getDataTable(list);
    }

    /**
     * 导出开关机信息列表
     */
    @RequiresPermissions("product:switch:export")
    @Log(title = "开关机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductSwitch productSwitch)
    {
        List<ProductSwitch> list = productSwitchService.selectProductSwitchList(productSwitch);
        ExcelUtil<ProductSwitch> util = new ExcelUtil<ProductSwitch>(ProductSwitch.class);
        return util.exportExcel(list, "开关机信息数据");
    }

    /**
     * 新增开关机信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存开关机信息
     */
    @RequiresPermissions("product:switch:add")
    @Log(title = "开关机信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductSwitch productSwitch)
    {
        return toAjax(productSwitchService.insertProductSwitch(productSwitch));
    }

    /**
     * 修改开关机信息
     */
    @RequiresPermissions("product:switch:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") String deviceId, ModelMap mmap)
    {
        ProductSwitch productSwitch = productSwitchService.selectProductSwitchByDeviceId(deviceId);
        mmap.put("productSwitch", productSwitch);
        return prefix + "/edit";
    }

    /**
     * 修改保存开关机信息
     */
    @RequiresPermissions("product:switch:edit")
    @Log(title = "开关机信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductSwitch productSwitch)
    {
        return toAjax(productSwitchService.updateProductSwitch(productSwitch));
    }

    /**
     * 删除开关机信息
     */
    @RequiresPermissions("product:switch:remove")
    @Log(title = "开关机信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productSwitchService.deleteProductSwitchByDeviceIds(ids));
    }
}
